
package com.jf.cloud.group.controller.app;


import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.group.constant.GroupActivityStatusEnum;
import com.jf.cloud.group.service.GroupActivityService;
import com.jf.cloud.group.service.GroupTeamService;
import com.jf.cloud.group.vo.app.AppGroupActivityVO;
import com.jf.cloud.group.vo.app.AppGroupTeamVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;


/**
 * 拼团活动表
 *
 * @author zz
 * @date 2021-03-20 10:39:31
 */
@RestController("appGroupActivityController")
@RequestMapping("/ua/group_activity")
@Tag(name = "app-拼团活动表")
public class GroupActivityController {

    @Autowired
    private GroupActivityService groupActivityService;

    @Autowired
    private GroupTeamService groupTeamService;

    @GetMapping("/info")
    @Operation(summary = "拼团活动详情" , description = "获取对应商品的拼团活动信息")
    public ServerResponseEntity<AppGroupActivityVO> activityInfo(@RequestParam("spuId") Long spuId) {

        // 查询拼团活动信息
        AppGroupActivityVO groupActivity = groupActivityService.getAppGroupActivityInfo(spuId);

        // 前端看到这个状态码的时候，不用渲染活动页面了
        if (groupActivity == null || !Objects.equals(groupActivity.getStatus(), GroupActivityStatusEnum.ENABLE.value())) {
            return ServerResponseEntity.fail(ResponseEnum.ACTIVITY_END);
        }

        long now = System.currentTimeMillis();
        int activityStatus = 2;
        // 活动还没开始
        if (groupActivity.getStartTime().getTime() > now) {
            // 距离活动开始还有
            groupActivity.setStartIn((groupActivity.getStartTime().getTime() - now) / 1000);
            activityStatus = 1;
        }
        // 活动还没结束
        if (groupActivity.getEndTime().getTime() > now) {
            // 距离活动开始还有
            groupActivity.setExpiresIn((groupActivity.getEndTime().getTime() - now) / 1000);
        }else{
            activityStatus = 3;
        }
        groupActivity.setActivityStatus(activityStatus);
        return ServerResponseEntity.success(groupActivity);
    }

    @GetMapping("/join_group_list")
    @Operation(summary = "可加入的团列表" , description = "只显示最近n个团列表(默认10)")
    public ServerResponseEntity<List<AppGroupTeamVO>> joinGroupList(
            @Parameter(description = "拼团活动ID", required = true) @RequestParam("groupActivityId") Long groupActivityId,
            @Parameter(description = "显示数量（默认10）") @RequestParam(value = "showSize", defaultValue = "10") Integer showSize) {
        List<AppGroupTeamVO> list = groupTeamService.listJoinGroup(groupActivityId, showSize);
        return ServerResponseEntity.success(list);
    }
}
